{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Welcome to SimpleITK Jupyter Notebooks\n",
    "\n",
    "\n",
    "## Newcomers to Jupyter Notebooks:\n",
    "1. We use two types of cells, code and markdown.\n",
    "2. To run a code cell, select it (mouse or arrow key so that it is highlighted) and then press shift+enter which also moves focus to the next cell or ctrl+enter which doesn't.\n",
    "3. Closing the browser window does not close the Jupyter server. To close the server, go to the terminal where you ran it and press ctrl+c twice.\n",
    "\n",
    "For additional details see the [Jupyter project documentation](https://jupyter.org/documentation) on Jupyter Notebook or JupyterLab.\n",
    "\n",
    "## SimpleITK Environment Setup\n",
    "\n",
    "Check that SimpleITK and auxiliary program(s) are correctly installed in your environment, and that you have the SimpleITK version which you expect (<b>requires network connectivity</b>).\n",
    "\n",
    "You can optionally download all of the data used in the notebooks in advance. This step is only necessary if you expect to run the notebooks without network connectivity."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The following cell checks that all expected packages are installed."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import importlib\n",
    "from distutils.version import LooseVersion\n",
    "\n",
    "# check that all packages are installed (see requirements.txt file)\n",
    "required_packages = {\n",
    "    \"jupyter\",\n",
    "    \"numpy\",\n",
    "    \"matplotlib\",\n",
    "    \"ipywidgets\",\n",
    "    \"scipy\",\n",
    "    \"pandas\",\n",
    "    \"numba\",\n",
    "    \"multiprocess\",\n",
    "    \"SimpleITK\",\n",
    "}\n",
    "\n",
    "problem_packages = list()\n",
    "# Iterate over the required packages: If the package is not installed\n",
    "# ignore the exception.\n",
    "for package in required_packages:\n",
    "    try:\n",
    "        p = importlib.import_module(package)\n",
    "    except ImportError:\n",
    "        problem_packages.append(package)\n",
    "\n",
    "if len(problem_packages) == 0:\n",
    "    print(\"All is well.\")\n",
    "else:\n",
    "    print(\n",
    "        \"The following packages are required but not installed: \"\n",
    "        + \", \".join(problem_packages)\n",
    "    )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import SimpleITK as sitk\n",
    "\n",
    "%run update_path_to_download_script\n",
    "from downloaddata import fetch_data, fetch_data_all\n",
    "\n",
    "from ipywidgets import interact\n",
    "\n",
    "print(sitk.Version())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We expect that you have an external image viewer installed. The default viewer is <a href=\"https://fiji.sc/#download\">Fiji</a>. If you have another viewer (i.e. ITK-SNAP or 3D Slicer) you will need to set an environment variable to point to it. This can be done from within a notebook as shown below."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "simpleitk_error_allowed": "Exception thrown in SimpleITK Show:"
   },
   "outputs": [],
   "source": [
    "# Uncomment the line below to change the default external viewer to your viewer of choice and test that it works.\n",
    "# %env SITK_SHOW_COMMAND /Applications/ITK-SNAP.app/Contents/MacOS/ITK-SNAP\n",
    "\n",
    "# Retrieve an image from the network, read it and display using the external viewer.\n",
    "# The show method will also set the display window's title and by setting debugOn to True,\n",
    "# will also print information with respect to the command it is attempting to invoke.\n",
    "# NOTE: The debug information is printed to the terminal from which you launched the notebook\n",
    "#       server.\n",
    "sitk.Show(sitk.ReadImage(fetch_data(\"SimpleITK.jpg\")), \"SimpleITK Logo\", debugOn=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now we check that the ipywidgets will display correctly. When you run the following cell you should see a slider.\n",
    "\n",
    "If you don't see a slider please shutdown the Jupyter server, at the command line prompt press Control-c twice, and then run the following command (jupyter notebook 5.2 or earlier):\n",
    "\n",
    "```jupyter nbextension enable --py --sys-prefix widgetsnbextension```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "interact(lambda x: x, x=(0, 10));"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Download all of the data in advance if you expect to be working offline (may take a couple of minutes)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "fetch_data_all(os.path.join(\"..\", \"Data\"), os.path.join(\"..\", \"Data\", \"manifest.json\"))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
